package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import model.Game;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import snake.GameEngine;
import snake.GamePackage;
import snake.GamePackage.GameStatus;
import snake.Player;
import snake.Player.Direction;
import utils.LoggedUser;

@Controller
public class GameController
{
	@RequestMapping("/startGame")
	public String startGame(HttpServletRequest request)
	{
		if (LoggedUser.isLogged(request.getSession()))
		{
			List<GamePackage> gamePackageList = (List<GamePackage>) request.getServletContext().getAttribute("startedGames");

			if (gamePackageList == null)
			{
				gamePackageList = new ArrayList<GamePackage>();
				request.getServletContext().setAttribute("startedGames", gamePackageList);
			}

			GamePackage gamePackage = new GamePackage();

			if (request.getSession().getAttribute("gameId") != null)
			{
				int gameId = (int) request.getSession().getAttribute("gameId");
				gamePackage.setGameId(gameId);
				
				gamePackage.setGameStatus(GameStatus.WAITING);

				if (((List<Game>) request.getServletContext().getAttribute("games")).get(gameId) != null)
				{
					Game currentGame = ((List<Game>) request.getServletContext().getAttribute("games")).get(gameId);
					gamePackage.setPlayersList(currentGame.getPlayerList());
					gamePackage.setScoreLimit(currentGame.getScoreLimit());
					gamePackageList.add(gamePackage);
					GameEngine ge=new GameEngine(gameId, gamePackage, request.getServletContext());
					
					WebSocketController.gameEngineList.put(gameId, ge);
					
					Thread t = new Thread(ge);
					t.start();

					return "gamePage";
				}
				return "mainPage";

			} else
			{
				return "mainPage";
			}

		}

		return "index";
	}

	@RequestMapping(value = "getGamePackage")
	public @ResponseBody GamePackage updateBoard(HttpServletRequest request)
	{

		if (LoggedUser.isLogged(request.getSession()) && request.getSession().getAttribute("gameId") != null && request.getParameter("userId") != null)
		{
			int userId = Integer.parseInt(request.getParameter("userId"));
			String direction = request.getParameter("direction");

			int gameId = (int) request.getSession().getAttribute("gameId");

			if (request.getServletContext().getAttribute("startedGames") != null)
			{
				List<GamePackage> gamePackageList = (List<GamePackage>) request.getServletContext().getAttribute("startedGames");

				GamePackage gamePackage = null;

				for (GamePackage gp : gamePackageList)
				{
					if (gp.getGameId() == gameId)
					{
						gamePackage = gp;
					}
				}

				if (gamePackage == null)
				{
					return null;
				}
				if (userId == LoggedUser.getUser(request.getSession()).getId())
				{

					for (Player player : gamePackage.getPlayersList())
					{
						if (player.getId() == userId)
						{
							if (Direction.valueOf(direction) == Direction.NORTH && player.getDirection() == Direction.SOUTH
									|| Direction.valueOf(direction) == Direction.EAST && player.getDirection() == Direction.WEST
									|| Direction.valueOf(direction) == Direction.SOUTH && player.getDirection() == Direction.NORTH
									|| Direction.valueOf(direction) == Direction.WEST && player.getDirection() == Direction.EAST)
							{
								System.out.println("dir:"+Direction.valueOf(direction)+" p:"+player.getDirection());
								return gamePackage;
							} else
							{
								
								Direction dr = Direction.valueOf(direction);
								player.setDirection(dr);
								return gamePackage;
							}
						}
					}
				}
			}
		}
		return null;
	}

	@RequestMapping("/joinGame")
	public String joinGame(HttpServletRequest request)
	{
		if (LoggedUser.isLogged(request.getSession()))
		{
			List<GamePackage> gamesList = (List<GamePackage>) request.getServletContext().getAttribute("startedGames");
			for (GamePackage gp : gamesList)
			{
				if (gp.getGameId() == (Integer) request.getSession().getAttribute("gameId"))
				{
					return "gamePage";
				}
			}
		}

		return "index";

	}

	@RequestMapping("/leaveGame")
	public String leaveGame(HttpServletRequest request)
	{

		if (LoggedUser.isLogged(request.getSession()))
		{
			return "mainPage";
		}
		return "index";
	}
	
	@RequestMapping("/gameOver")
	public @ResponseBody void gameOver(HttpServletRequest request)
	{
		Integer gameId = (Integer) request.getSession().getAttribute("gameId");

		List<Game> games = (List<Game>) request.getServletContext().getAttribute("games");

		List<GamePackage> gamesList = (List<GamePackage>) request.getServletContext().getAttribute("startedGames");

		if (gameId != null)
			if (games.size() > 0)
			{
				games.remove(gameId);

				for (int i = 0; i < games.size(); i++)
				{
					if (games.get(i).getId() == gameId)
					{
						games.remove(i);
					}
				}

				for (int i = 0; i < gamesList.size(); i++)
				{
					if (gamesList.get(i).getGameId() == gameId)
					{
						gamesList.remove(i);					
					}
				}
				request.getSession().removeAttribute("gameId");

				
			} else
			{
				request.getSession().removeAttribute("gameId");
			
			}
	
	}
}
